﻿// -----------------------------------------------------------------------
// <copyright file="DmKhoaDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmKhoaDataAccess
    {
        public bool InsertDmKhoa(DmKhoaEntity obj)
        {
            try
            {
                string sql = "spDmKhoa_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "TenKhoa", DbType.String, obj.TenKhoa);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmKhoa", ex.Message); throw ex; }
        }

        public bool EditDmKhoa(DmKhoaEntity obj)
        {
            try
            {
                string sql = "spDmKhoa_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, obj.MaKhoa);
                db.AddInParameter(cmd, "TenKhoa", DbType.String, obj.TenKhoa);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmKhoa", ex.Message); throw ex; }
        }

        public bool DeleteDmKhoa(string maKhoa)
        {
            try
            {
                string sql = "spDmKhoa_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, maKhoa);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmKhoa", ex.Message); throw ex; }
        }

        public DmKhoaEntity GetDmKhoa(string maKhoa)
        {
            try
            {
                DmKhoaEntity obj = null;
                string sql = "spDmKhoa_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaKhoa", DbType.String, maKhoa);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmKhoaEntity();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.TenKhoa = reader["TenKhoa"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmKhoa", ex.Message); throw ex; }
        }

        public List<DmKhoaEntity> GetDmKhoaList()
        {
            try
            {
                List<DmKhoaEntity> list = new List<DmKhoaEntity>();
                DmKhoaEntity obj = null;
                string sql = "spDmKhoa_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmKhoaEntity();
                        obj.MaKhoa = reader["MaKhoa"].ToString();
                        obj.TenKhoa = reader["TenKhoa"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmKhoaList", ex.Message); throw ex; }
        }

    }
}